package com.by.basicgateway.intercept.exception;

import cn.hutool.core.bean.BeanUtil;
import com.by.toolkit.enums.ErrorCodeEnum;
import com.by.toolkit.model.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.Map;

@Slf4j
public class GlobalErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler {

    public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) {
        super(errorAttributes, resourceProperties, errorProperties, applicationContext);
    }

    //返回前端数据
    @Override
    protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
        Resp resp = Resp.builder()
                .code(ErrorCodeEnum.GATE_ERROR.getErrorCode())
                .msg(ErrorCodeEnum.GATE_ERROR.getErrorMessage())
                .build();
        return BeanUtil.beanToMap(resp);
    }
}
